package org.funtester.performance.books.chapter02.section8;

/**
 * ThreadLocal 演示
 */
public class ThreadLocalDemo {

    public static void main(String[] args) {
        ThreadLocal<String> threadLocal = new ThreadLocal<String>() {// 创建一个线程本地变量
            @Override
            protected String initialValue() { // 重写初始化方法
                return "Hello FunTester !";
            }
        };
        for (int i = 0; i < 3; i++) {
            new Thread(() -> { // 创建并启动3个线程
                System.out.println("初始值: " + threadLocal.get()); // 获取本地变量值,并输出
                threadLocal.set(Thread.currentThread().getName() + "    Hello FunTester !"); // 设置本地变量值
                System.out.println("修改后值: " + threadLocal.get()); // 获取本地变量值,并输出
            }).start();
        }
    }

}
